﻿// -----------------------------------------------------------------------
// <copyright file="Job_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Job_DAL : BaseDAL
    {
        public static List<Job> GetDataByEquipmentID(int id, int shipID, bool isChanged)
        {
            if (isChanged)
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Jobs);
            }
            return DB.Jobs.Where(n=>n.Equipment.Ship_ID == shipID && n.Equipment_ID == id).ToList();
        }

        public static List<Job> GetAll(int shipID)
        {
            return DB.Jobs.Where(n=>n.Equipment.Ship_ID == shipID).ToList();
        }

        // Lấy Job theo Id
        public static Job GetDataById(int id)
        {
            return DB.Jobs.Where(m => m.Job_ID == id).FirstOrDefault();
        }
        //Thêm một Job
        public static bool Insert(Job job)
        {
            try
            {
                DB.Jobs.InsertOnSubmit(job);
                DB.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                string str = ex.ToString();
                return false;
            }

        }
        public static Job TestExitsJobDescription(int id, int jobDescription)
        {
            return DB.Jobs.Where(n => n.Equipment_ID == id && n.JobDescription_ID == jobDescription).FirstOrDefault();
        }
        public static Job GetDataByName(string name, int shipID)
        {
            return DB.Jobs.Where(n => n.JobDescription.NameJD == name && n.Equipment.Ship_ID == shipID).FirstOrDefault();
        }
        public static List<Job> GetJobInRound(int roundID)
        {
          //return DB.Jobs.Where(n => n.Round_ID == roundID).ToList();
            return null;
        }
        public static Job GetDataByNumber(string code)
        {
            return DB.Jobs.Where(n => n.JobDescription.CodeJD == code).FirstOrDefault();
        }
        public static Job GetDataByDesc(string Description)
        {
            return DB.Jobs.Where(n => n.JobDescription.CodeJD == Description).FirstOrDefault();
        }
        public static Job GetLastJob()
        {
            List<Job> listJob = DB.Jobs.ToList();
            return listJob[listJob.Count - 1];
        }

        // Xóa một Job
        public static bool Delete(Job job)
        {
            try
            {
                DB.Jobs.DeleteOnSubmit(job);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        public static bool TestBeforeDelete(Job job)
        {
            JobPart jp = DB.JobParts.Where(n => n.Job_ID == job.Job_ID).FirstOrDefault();
            if (jp != null)
                return false;
            WorkOrder work = DB.WorkOrders.Where(n => n.Job_ID == job.Job_ID).FirstOrDefault();
            if (work != null)
                return false;
            AttachmentsJob attach = DB.AttachmentsJobs.Where(n => n.Job_ID == job.Job_ID).FirstOrDefault();
            if (attach != null)
                return false;
            RoundJob roundJob = DB.RoundJobs.Where(n => n.Job_ID == job.Job_ID).FirstOrDefault();
            if (roundJob != null)
                return false;


            return true;
        }
        public static bool DeleteByID(int id)
        {
            try
            {
                Job j = GetDataById(id);
                DB.Jobs.DeleteOnSubmit(j);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        // Cập nhật Job
        public static bool Update(Job job)
        {
            try
            {
                Job objJob = GetDataById(job.Job_ID);

                objJob.Details = job.Details;
                objJob.Equipment_ID = job.Equipment_ID;
                objJob.FreodicFrequency = job.FreodicFrequency;
                objJob.JobDescription_ID = job.JobDescription_ID;
                objJob.Counter = job.Counter;
                objJob.SC_ID = job.SC_ID;

                objJob.LastDone = job.LastDone;
                objJob.NextDue = job.NextDue;
                objJob.TimeUnit = job.TimeUnit;
                objJob.AlarmDaybefore = job.AlarmDaybefore;

                objJob.TimeDone = job.TimeDone;

                DB.SubmitChanges();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }

        }

        // Kiểm tra xem Job Code có tồn tại chưa ?
        public static bool IsExits(Job job, int shipID)
        {
            Job objJob = null;
            objJob = DB.Jobs.Where(m => (m.JobDescription_ID == job.JobDescription_ID) &&
                (m.Job_ID != job.Job_ID) && (m.Equipment_ID == job.Equipment_ID) && (m.Equipment.Ship_ID == shipID)).FirstOrDefault();

            return objJob == null ? true : false;
        }
    }
}
